//
//  SectionHeaderGroup.swift
//  AICleaner
//
//  Created by firefly on 2025/1/26.
//

import SwiftUI

/// 分组视图，支持自定义子控件

/// DeepSeek优化-基于手写
struct SectionHeaderGroup<Indicator: View, Child: View>: View {
    let title: String
    let titleFont: Font
    let indicatorColor: Color
    let vStackSpacing: CGFloat
    let hStackSpacing: CGFloat
    let bottomMargin: CGFloat
    
    @ViewBuilder let indicatorContent: () -> Indicator
    @ViewBuilder let childContent: () -> Child
    
    init(
        title: String,
        titleFont: Font = .title3.bold(),
        indicatorColor: Color = .blue,
        vStackSpacing: CGFloat = 12,
        hStackSpacing: CGFloat = 8,
        bottomMargin: CGFloat = 10,
        @ViewBuilder indicatorContent: @escaping () -> Indicator = { EmptyView() },
        @ViewBuilder childContent: @escaping () -> Child
    ) {
        self.title = title
        self.titleFont = titleFont
        self.indicatorColor = indicatorColor
        self.vStackSpacing = vStackSpacing
        self.hStackSpacing = hStackSpacing
        self.bottomMargin = bottomMargin
        self.indicatorContent = indicatorContent
        self.childContent = childContent
    }
    
    var body: some View {
        VStack(alignment: .leading, spacing: vStackSpacing) {
            headerView
            childContent()
            Spacer().frame(height: bottomMargin)
        }
    }
    
    private var headerView: some View {
        // 底部文本基线对齐 .firstTextBaseline
        HStack(alignment: .center, spacing: hStackSpacing) {
            Group {
                if !(Indicator.self == EmptyView.self) {
                    indicatorContent()
                } else {
                    defaultIndicator
                }
            }
            
            Text(title)
                .font(titleFont)
            
            Spacer()
        }
    }
    
    private var defaultIndicator: some View {
        Rectangle()
            .fill(indicatorColor)
            .frame(width: 6, height: 20)
            .cornerRadius(2)
    }
}

#Preview {
    SectionHeaderGroup(
        title: "项目进度",
        indicatorColor: .green
    ) {
        ProgressView(value: 0.75)
            .tint(.green)
    }
}
/// Kimi优化-基于手写
//struct SectionHeaderGroup<Content: View>: View {
//    var title: String
//    @ViewBuilder let indicatorView: () -> Content?
//    @ViewBuilder let child: () -> Content
//
//    var body: some View {
//        VStack(alignment: .leading) {
//            HStack {
//                if let indicatorView = indicatorView() {
//                    indicatorView.frame(width: 10, height: 20)
//                        .cornerRadius(8)
//                } else {
//                    Color.blue.frame(width: 10, height: 20)
//                        .cornerRadius(8)
//                }
//                
//                Spacer().frame(width: 10)
//                Text(title)
//                    .font(.title)
//            }
//            
//            Spacer().frame(height: 10)
//            
//            child()
//            
//            Spacer().frame(height: 10)
//        }
//    }
//}

/// 手写
//struct SectionHeaderGroup<Content:View>: View {
//    var title: String
//    @ViewBuilder let indicatorView:() -> Content?
//    // 占位视图
//    @ViewBuilder let child:() -> Content
//    
//    var body: some View {
//        VStack(alignment: .leading){
//            HStack {
//                if let indicatorView = indicatorView() {
//                    indicatorView.frame(width: 10, height: 20)
//                        .cornerRadius(8, corners: .allCorners)
//                } else {
//                    Color.blue.frame(width: 10, height: 20)
//                        .cornerRadius(8, corners: .allCorners)
//                }
//                
//                Spacer().frame(width: 10)
//                Text(title)
//                    .font(.title)
//            }
//            
//            Spacer().frame(height: 10)
//            
//            child()
//            
//            Spacer().frame(height: 10)
//        }
//    }
//}
